﻿using System;

namespace Manoso.Data.FluentMap
{
    public interface IMapColumn
    {
        /// <summary>
        /// Get/Set column name.
        /// </summary>
        string Name { get; set; }

        /// <summary>
        /// Get table alias.
        /// </summary>
        string TableAlias { get; }

        /// <summary>
        /// Get column alias.
        /// </summary>
        string Alias { get; }

        /// <summary>
        /// Get/Set a reference for the column.
        /// </summary>
        string Reference { get; set; }

        /// <summary>
        /// Get/Set the column value.
        /// </summary>
        object Value { get; set; }
    }

    internal class MapColumn : IMapColumn
    {
        private const string COLUMN_SPLIT = "_";

        private string _name;
        public string Name
        {
            get { return _name; }
            set 
            { 
                _name = value;
                _alias = null;
            }
        }

        public string TableAlias { get; private set; }

        private string _alias;
        public string Alias
        {
            get
            {
                return _alias ?? (_alias = String.IsNullOrWhiteSpace(TableAlias) ? Name : String.Format("{0}{1}{2}", TableAlias, COLUMN_SPLIT, Name));
            }
        }

        public object Value { get; set; }
        public string Reference { get; set; }

        public MapColumn(string name, string tableAlias)
        {
            Name = name;
            TableAlias = tableAlias;
        }
    }
}
